package com.giscloud.limp.service.impl;

import com.giscloud.token.utils.TokenUserUtil;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.commons.model.Result;
import cn.hutool.core.collection.CollectionUtil;

import java.util.Date;
import java.util.List;
import com.giscloud.limp.entity.CaseWarnExample;
import com.giscloud.limp.entity.dto.CaseWarnDTO;
import com.giscloud.limp.dao.CaseWarnDao;
import com.giscloud.limp.entity.CaseWarn;
import com.giscloud.limp.service.CaseWarnService;
import lombok.extern.log4j.Log4j2;


@Service("caseWarnService")
@Log4j2
public class CaseWarnServiceImpl implements CaseWarnService {
    private final  CaseWarnDao caseWarnDao;
    @Autowired
    private TokenUserUtil tokenUserUtil;
    @Autowired
    public CaseWarnServiceImpl(CaseWarnDao caseWarnDao) {
        this.caseWarnDao = caseWarnDao;
    }
    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(CaseWarnDTO caseWarnDTO) {
        log.debug("#queryPage parameter ={}", JSON.toJSONString(caseWarnDTO));
        PageHelper.startPage(caseWarnDTO.getPage(), caseWarnDTO.getRows());
        CaseWarnExample caseWarnExample = new CaseWarnExample();
        CaseWarnExample.Criteria criteria = caseWarnExample.createCriteria();
        List<CaseWarn> caseWarns = caseWarnDao.selectByExample(caseWarnExample);
        return PageInfo.of(caseWarns);
    }


    /**
     * 信息
     */
    @Override
    public CaseWarn getById(Integer caseWarnId){
        log.debug("#getById parameter ={}", caseWarnId);
        return caseWarnDao.selectByPrimaryKey(caseWarnId);
    }

    /**
     * 保存
     */
    @Override

    public Result save(CaseWarn caseWarn){
        log.debug("#save parameter ={}", JSON.toJSONString(caseWarn));
        caseWarn.setCreatedStaffId(tokenUserUtil.getUserId().intValue());
        caseWarn.setCreatedDt(new Date());
        if(caseWarnDao.insertSelective(caseWarn) > 0 ){
            return Result.success("保存成功");
        }else{
            return Result.error("保存失败");
        }
    }

    /**
     * 修改
     */
    @Override
    public Result updateById(CaseWarn caseWarn){
         log.debug("#updateById parameter ={}", JSON.toJSONString(caseWarn));
        caseWarn.setOperateStaffId(tokenUserUtil.getUserId().intValue());
        caseWarn.setVersion(new Date());
         CaseWarnExample caseWarnExample = new CaseWarnExample();
         CaseWarnExample.Criteria criteria = caseWarnExample.createCriteria();
         if(caseWarn.getCaseWarnId() != null){
             criteria.andCaseWarnIdEqualTo(caseWarn.getCaseWarnId());
         }else{
             criteria.andCaseIdEqualTo(caseWarn.getCaseId());
             criteria.andLitigationStageEqualTo(caseWarn.getLitigationStage());
         }

         if(caseWarnDao.updateByExampleSelective(caseWarn,caseWarnExample) > 0){
             return Result.success("修改成功");
         }else{
             return Result.error("修改失败");
         }
    }
    /**
     * 删除
     */
    @Override

    public Result removeByIds(List<Integer> caseWarnIds){
        log.debug("#removeByIds parameter ={}", JSON.toJSONString(caseWarnIds));
        CaseWarnExample caseWarnExample = new CaseWarnExample();
        CaseWarnExample.Criteria criteria = caseWarnExample.createCriteria();

            criteria.andCaseWarnIdIn(caseWarnIds);
      
        if(caseWarnDao.deleteByExample(caseWarnExample) > 0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

    /**
     * 删除
     */
    @Override
    public Result remove(CaseWarn caseWarn){
        CaseWarnExample caseWarnExample = new CaseWarnExample();
        CaseWarnExample.Criteria criteria = caseWarnExample.createCriteria();
        if(caseWarn.getCaseWarnId() != null){
            criteria.andCaseWarnIdEqualTo(caseWarn.getCaseWarnId());
        }else{
            criteria.andCaseIdEqualTo(caseWarn.getCaseId());
            criteria.andLitigationStageEqualTo(caseWarn.getLitigationStage());
        }
        if(caseWarnDao.deleteByExample(caseWarnExample) > 0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

}